package ch.zbw.sysventory.server.scanner.adapter.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ch.zbw.sysventory.server.model.Computer;
import ch.zbw.sysventory.server.model.ComputerScan;
import ch.zbw.sysventory.server.model.ComputerScanResult;
import ch.zbw.sysventory.server.model.NetworkAdapter;
import ch.zbw.sysventory.server.model.Printer;
import ch.zbw.sysventory.server.model.Software;
import ch.zbw.sysventory.server.scanner.adapter.ScanResultAdapter;
import ch.zbw.sysventory.server.service.ComputerScanResultService;
import ch.zbw.sysventory.server.service.NetworkAdapterService;
import ch.zbw.sysventory.server.service.PrinterService;
import ch.zbw.sysventory.server.service.SoftwareService;
import ch.zbw.sysventory.transport.model.ScanResult;

@Component
public class ScanResultAdapterImpl implements ScanResultAdapter {

	@Autowired
	private SoftwareService softwareService;

	@Autowired
	private PrinterService printerService;

	@Autowired
	private NetworkAdapterService networkAdapterService;

	@Autowired
	private ComputerScanResultService computerScanResultService;

	@Override
	public void importScanResult(ScanResult scanResult, String scanIpAddress, Computer computer, ComputerScan computerScan) {
		if (scanResult != null) {
			ComputerScanResult computerScanResult = new ComputerScanResult();
			computerScanResult.setComputerScan(computerScan);
			computerScanResult.setTimeStamp(scanResult.getTimeStamp());
			computerScanResult.setScanIpAddress(scanIpAddress);

			computerScanResult.setComputer(computer);
			computerScanResult.setComputerName(scanResult.getComputerName());
			computerScanResult.setComputerType(scanResult.getComputerType());
			computerScanResult.setCpu(scanResult.getCpu());
			computerScanResult.setOperatingSystem(scanResult.getOperatingSystem());
			computerScanResult.setRam(scanResult.getRam());
			computerScanResult.setSid(scanResult.getSid());

			computerScanResult.setSoftwareList(this.mapSoftwareList(scanResult.getSoftware()));
			computerScanResult.setPrinterList(this.mapPrinterList(scanResult.getPrinters()));
			computerScanResult.setNetworkAdapterList(this.mapNetworkAdapterList(scanResult.getNetworkAdapters(), scanIpAddress));

			this.computerScanResultService.save(computerScanResult);
		}
	}

	private List<Software> mapSoftwareList(List<ch.zbw.sysventory.transport.model.Software> softwareList) {
		List<Software> resultList = new ArrayList<Software>();
		if (softwareList != null && softwareList.size() > 0) {
			Iterator<ch.zbw.sysventory.transport.model.Software> iterator = softwareList.iterator();
			while (iterator.hasNext()) {
				ch.zbw.sysventory.transport.model.Software software = iterator.next();
				resultList.add(this.softwareService.createIfNotExists(software.getName(), software.getVersion()));
			}
		}
		return resultList;
	}

	private List<Printer> mapPrinterList(List<ch.zbw.sysventory.transport.model.Printer> printerList) {
		List<Printer> resultList = new ArrayList<Printer>();
		if (printerList != null && printerList.size() > 0) {
			Iterator<ch.zbw.sysventory.transport.model.Printer> iterator = printerList.iterator();
			while (iterator.hasNext()) {
				ch.zbw.sysventory.transport.model.Printer printer = iterator.next();
				resultList.add(this.printerService.createIfNotExists(printer.getName(), printer.getDriver(), printer.getDriverVersion()));
			}
		}
		return resultList;
	}

	private List<NetworkAdapter> mapNetworkAdapterList(List<ch.zbw.sysventory.transport.model.NetworkAdapter> networkAdapterList, String scanIpAddress) {
		List<NetworkAdapter> resultList = new ArrayList<NetworkAdapter>();
		if (networkAdapterList != null && networkAdapterList.size() > 0) {
			Iterator<ch.zbw.sysventory.transport.model.NetworkAdapter> iterator = networkAdapterList.iterator();
			while (iterator.hasNext()) {
				ch.zbw.sysventory.transport.model.NetworkAdapter networkAdapter = iterator.next();
				NetworkAdapter newNetworkAdapter = new NetworkAdapter();
				newNetworkAdapter.setDefaultGateway(networkAdapter.getDefaultGateway());
				newNetworkAdapter.setDhcpServer(networkAdapter.getDhcpServer());
				newNetworkAdapter.setIpAddress(networkAdapter.getIpAddress());
				newNetworkAdapter.setMacAddress(networkAdapter.getMacAddress());
				newNetworkAdapter.setName(networkAdapter.getName());
				newNetworkAdapter.setSubnet(networkAdapter.getSubnet());
				newNetworkAdapter.setAdapterWithScanIp(false);
				if (scanIpAddress != null && scanIpAddress.equals(newNetworkAdapter.getIpAddress()))
					newNetworkAdapter.setAdapterWithScanIp(true);
				resultList.add(this.networkAdapterService.save(newNetworkAdapter));
			}
		}
		return resultList;
	}

}
